package com.lanou.dllo.soccernews.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.bean.ReciverBean;
import com.lanou.dllo.soccernews.volley.GetVolleyDatasUtil;

import java.util.List;

/**
 * Created by dllo on 15/11/9.
 */
public class ReceiverAdapter extends BaseAdapter {

    List<ReciverBean.DataCircleResult> dataCircleResults;

    private LayoutInflater mInflater;
    private Context context;

    public ReceiverAdapter(Context context) {
        mInflater = LayoutInflater.from(context);

        this.context = context;
    }

    public void addDatas(List<ReciverBean.DataCircleResult> dataCircleResults) {

        this.dataCircleResults = dataCircleResults;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return dataCircleResults != null && dataCircleResults.size() > 0 ? dataCircleResults.size()
                : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataCircleResults.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CentViewHodle hodle;
        if (convertView == null) {
            hodle = new CentViewHodle();
            convertView = mInflater.inflate(R.layout.item_detail_circle, parent, false);

            hodle.mm_content = (TextView) convertView.findViewById(R.id.mm_content);
            hodle.mm_floor = (TextView) convertView.findViewById(R.id.mm_floor);
            hodle.mm_up = (TextView) convertView.findViewById(R.id.mm_up);
            hodle.mm_username = (TextView) convertView.findViewById(R.id.mm_username);
            hodle.mm_quota = (TextView) convertView.findViewById(R.id.mm_quota);


            hodle.mm_receiver_username = (TextView) convertView.findViewById(R.id.mm_receiver_username);
            hodle.cardView = (CardView) convertView.findViewById(R.id.cardview);
//            hodle.points = (TextView) convertView.findViewById(R.id.cent_item_points);
            hodle.mm_avatar = (ImageView) convertView.findViewById(R.id.mm_username_log);
            hodle.mm_receiver_user_avatar = (ImageView) convertView.findViewById(R.id.mm_receiver_log);
            convertView.setTag(hodle);
        } else {
            hodle = (CentViewHodle) convertView.getTag();
        }
        ReciverBean.DataCircleResult dataCircleResult = dataCircleResults.get(position);

        if (dataCircleResult != null) {
            // Log.i("lanou", rankingsEntity.getRank() + "****");
            hodle.mm_content.setText(dataCircleResult.getContent());
            hodle.mm_floor.setText(dataCircleResult.getFloor() + "楼");
            hodle.mm_up.setText(dataCircleResult.getUp()+"");
            hodle.mm_username.setText(dataCircleResult.getAuthor().getUsername());

            String authorUrl = dataCircleResult.getAuthor().getAvatar();
            if (authorUrl != null && authorUrl.length() > 0)

            {
                GetVolleyDatasUtil.getNetWorkImg(authorUrl, hodle.mm_avatar);
            }


            String content = dataCircleResult.getQuota();
            if (content != null) {
                hodle.cardView.setVisibility(View.VISIBLE);
                ReciverBean.DataCircleResult.Receiver receiver = dataCircleResult.getReceiver();
                hodle.mm_receiver_username.setText(receiver.getUsername());
                hodle.mm_quota.setText(dataCircleResult.getQuota());
                String reciverUrl = receiver.getAvatar();
                if (reciverUrl != null && reciverUrl.length() > 0) {
                    GetVolleyDatasUtil.getNetWorkImg(reciverUrl, hodle.mm_receiver_user_avatar);
                }


            }

        }

        return convertView;
    }

    class CentViewHodle {

        private TextView mm_content, mm_floor, mm_up, mm_username, mm_quota, mm_receiver_username;
        private ImageView mm_avatar, mm_receiver_user_avatar;
        private CardView cardView;
    }
}
